package com.yuntsg.subject_database.auxiliary;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.yuntsg.subject_database.entity.PublicEntity;
import com.yuntsg.subject_database.util.OftenUtils;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 此继续扫尾表添加年份
 */
public class ReadMysqlTest {
    public static void main(String[] args) throws SQLException, IOException {

        String readTable = "dir.ncbijournalpdf_20220224_fullpath";
        String saveTable = "dir.cases_20220304_2021_12_01_2022_03_04_all";

        Map<String, Integer> strIntMap = new ConcurrentHashMap<>();
        Set<Integer> pmidSet = new ConcurrentHashSet<>();

        String limit = "";
//        String limit = " limit 1";
        int onePageNum = 1000;

        readTable(readTable, onePageNum, limit, "pid", "file_id", "read", strIntMap, pmidSet, true);
        readTable(saveTable, onePageNum, limit, "id", "pmid", "save", strIntMap, pmidSet, true);

// main
    }


    // 读七个实体类表，获取国家、省份、城市、论文信息（articleInfo）四个数据，获取NCBI的作者全写与简写
    public static void readTable(String readTable, Integer onePageNum, String limit, String autoId, String field, String type, Map<String, Integer> strIntMap, Set<Integer> pmidSet, boolean print) throws SQLException, IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Integer> tempNum = Collections.synchronizedList(new ArrayList<>());
        int from = 1;
        int max = 0;
        List<Entity> tableNum = Db.use().query("select count(1) as count from " + readTable);
        int count = tableNum.get(0).getInt("count");
        if (count > 0) {
            max = Db.use().queryNumber("select max(" + autoId + ") from " + readTable).intValue();
        }
        int page = max / onePageNum;
        if (max % onePageNum != 0) {
            page++;
        }
        List<PublicEntity.PageInfo> pageInfos = new ArrayList<>();
        for (int i = 1; i <= page; i++) {
            int start = (i - 1) * onePageNum + from;
            int stop = i * onePageNum + from - 1;
            if (stop > max) {
                stop = max + onePageNum;
            }
            pageInfos.add(new PublicEntity.PageInfo(start, stop));
        }
        int numAll = pageInfos.size();
        pageInfos.parallelStream().forEach(pg -> {
            try {
                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + readTable + " where " + autoId + " between " + pg.getFrom() + " and " + pg.getTo() + limit);
                tempNum.add(pg.getFrom());
                if (print && tempNum.size() % 1000 == 0) {
                    System.out.println(format.format(new Date()) + " 现在开始： " + pg.getFrom() + " -- " + pg.getTo() + " 总条数： " + numAll + " 目前已处理： " + tempNum.size() + " 表名： " + readTable);
                }
                for (Entity entry : tableData) {
                    if (null != entry) {
                        if (type.equals("read")) {
                            String file_id = OftenUtils.getStrField(entry, "file_id");
                            if (file_id.startsWith("PM")) {
                                String pmid = file_id.substring("PM".length());
                                if (NumberUtil.isInteger(pmid)) {
                                    pmidSet.add(Integer.parseInt(pmid));
                                }
                            }

//                            String ut = OftenUtils.getStrField(entry, "UT").toUpperCase().replace("WOS:", "UT").trim();
//                            int year = OftenUtils.strToInteger(OftenUtils.getStrField(entry, "PY"));
//                            if (year > 1000 && year < 3000) {
//                                strIntMap.put(ut, year);
//                            }
                        }
                        if (type.equals("save")) {

                            Integer pmid = OftenUtils.getIntField(entry, "pmid");
                            if (!pmidSet.contains(pmid)) {
                                pmidSet.add(pmid);
                                try {
                                    Db.use().insert(Entity.create("dir.pmid").set("pmid", pmid));
                                } catch (SQLException throwables) {
                                    throwables.printStackTrace();
                                }
                            }

//                            int id = OftenUtils.getIntField(entry, autoId);
//                            String ut = OftenUtils.getStrField(entry, "file_id").toUpperCase();
//                            if (strIntMap.containsKey(ut)) {
//                                try {
//                                    Db.use().update(Entity.create().set("year", strIntMap.get(ut)), Entity.create(readTable).set("id", id));
//                                } catch (SQLException e) {
//                                    e.printStackTrace();
//                                }
//                            }
                        }
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
    }


// end
}
